package com.darrenchan.course06

import scala.util.Random

object MatchApp extends App{
  val names = Array("陈驰", "苏君君", "魏印福")
  val name = names(Random.nextInt(names.length))

  name match {
    case "陈驰" => println("chenchi")
    case "苏君君" => println("sjj")
    case _ => println("wyf")
  }

  def judgeGrade(name: String, grade: String): Unit = {
    grade match {
      case "A" => println("Excellent")
      case "B" => println("Good")
      case "C" => println("Pass")
      case _ if ("陈驰".equals(name)) => println("有关系，通过")
      case _ => println("Failed")
    }
  }

  judgeGrade("陈驰", "A")
  judgeGrade("陈驰", "B")
  judgeGrade("陈驰", "C")
  judgeGrade("陈驰", "D")
  judgeGrade("魏印福", "D")



  def greeting(array: Array[String]): Unit = {
    array match {
      case Array("zhangsan") => println("Hi: zhangsan")
      case Array(x, y) => println("Hi: " + x + ", " + y)
      case Array("zhangsan", _*) => println("Hi: zhansan and other friends")
      case _ => println("Hi: everybody")
    }
  }

  greeting(Array("zhangsan1", "bbb", "ccc"))


  def matchType(obj: Any): Unit = {
    obj match {
      case x: Int => println("Int")
      case x: String => println("String")
      case x: Map[_, _] => x.foreach(x => println(x))
      case _ => println("other type")
    }
  }


  matchType(Map("name" -> "cc"))

  def matchCaseClass(person: Person): Unit = {
    person match {
      case CTO(name, floor) => println("CTO")
      case Employee(name, floor) => println("Employee")
      case Manager(name, floor) => println("Manager")
      case _ => println("other")
    }
  }


  class Person
  case class CTO(name: String, floor: String) extends Person
  case class Employee(name: String, floor: String) extends Person
  case class Manager(name: String, floor: String) extends Person

  matchCaseClass(Manager("xxx", "2L"))
}
